package com.heima.article.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.ApArticleConfigService;
import com.heima.article.service.ApArticleService;
import com.heima.common.exception.CustomException;
import com.heima.model.article.dtos.ArticleCommentConfigDto;
import com.heima.model.article.dtos.ArticleCommentDto;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.article.vos.ArticleCommentVo;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class ApArticleConfigServiceImpl extends ServiceImpl<ApArticleConfigMapper, ApArticleConfig> implements ApArticleConfigService {

    @Autowired
    private ApArticleConfigMapper apArticleConfigMapper;

    @Autowired
    private ApArticleService apArticleService;

    @Autowired
    private ApArticleMapper apArticleMapper;
    /**
     * 更新app端文章配置表的文章状态
     *
     * @param map
     */
    @Override
    public void updateByMap(Map map) {
        //1入参校验
        if (map == null) {
            return;
        }
        Boolean enable = Boolean.valueOf(map.get("enable") + "");
        boolean isDown = (!enable); //enable:0下架 1上架

        //2.更新数据表
        update(Wrappers.<ApArticleConfig>lambdaUpdate().eq(ApArticleConfig::getArticleId, map.get("articleId"))
                .set(ApArticleConfig::getIsDown, isDown));
    }

    /**
     * 查看评论列表
     *
     * @param dto
     * @return
     */
    @Override
    public PageResponseResult findNewsComments(ArticleCommentDto dto) {
        //查询article表和config表中数据
        //参数校验
        if (dto == null) {
            throw new CustomException(AppHttpCodeEnum.PARAM_INVALID);
        }
//        List<ApArticle> articleList = apArticleService.list(Wrappers.<ApArticle>lambdaQuery()
//                .between(ApArticle::getPublishTime,dto.getBeginDate(),dto.getEndDate())
//                .eq(ApArticle::getAuthorId,dto.getWmUserId()));
//        List<ArticleCommentVo> articleCommentVoList = new ArrayList<>();
//        for (ApArticle article : articleList) {
//            ArticleCommentVo articleCommentVo = new ArticleCommentVo();
//            articleCommentVo.setId(article.getId());
//            articleCommentVo.setTitle(article.getTitle());
//            if (article.getComment() == null) {
//                articleCommentVo.setComments(0);
//            } else {
//                articleCommentVo.setComments(article.getComment());
//            }
//            articleCommentVo.setPublishTime(article.getPublishTime());
//            ApArticleConfig articleConfig = getOne(Wrappers.<ApArticleConfig>lambdaQuery().eq(ApArticleConfig::getArticleId, article.getId()));
//            articleCommentVo.setIsComment(articleConfig.getIsComment());
//            articleCommentVoList.add(articleCommentVo);
//        }
        Integer currentPage = dto.getPage();
        dto.setPage((dto.getPage()-1)*dto.getSize());
        List<ArticleCommentVo> articleCommentVoList = apArticleConfigMapper.loadArticleCommentList(dto);
        PageResponseResult responseResult = new PageResponseResult(currentPage,dto.getSize(),articleCommentVoList.size());

        responseResult.setData(articleCommentVoList);
//        PageResponseResult pageResponseResult = new PageResponseResult();
//        pageResponseResult.setCurrentPage((int) page.getCurrent());
//        pageResponseResult.setSize((int) page.getSize());
//        pageResponseResult.setTotal((int) page.getTotal());
//        pageResponseResult.setData(articleCommentVoList);

        return responseResult;
    }

    /**
     * 根据文章id查询config
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult getConfig(Long id) {
        ApArticleConfig articleConfig = getOne(Wrappers.<ApArticleConfig>lambdaQuery().eq(ApArticleConfig::getArticleId, id));
        return ResponseResult.okResult(articleConfig);
    }

    /**
     * 设置评论状态
     * @param dto
     * @return
     */
    @Override
    public ResponseResult updateCommentStatus(ArticleCommentConfigDto dto) {
        //查询文章表,修改对应的评论数量 设置为0
//        apArticleService.update(Wrappers.<ApArticle>lambdaUpdate()
//                .eq(ApArticle::getId,dto.getArticleId())
//                        .set(ApArticle::getComment,0));
        ApArticle article = apArticleMapper.selectById(dto.getArticleId());
        article.setComment(0);
        apArticleMapper.updateById(article);
        //更加文章查询文章配置表数据,再设置状态
        update(Wrappers.<ApArticleConfig>lambdaUpdate()
                .eq(ApArticleConfig::getArticleId,dto.getArticleId())
                .set(ApArticleConfig::getIsComment,dto.getOperation()));
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
